package com.uw.tookit;

import java.util.ArrayList;  
import java.util.HashMap;  
import java.util.Map;

import android.app.Activity;  
import android.view.LayoutInflater;  
import android.view.View;  
import android.view.ViewGroup;  
import android.widget.BaseAdapter;  
import android.widget.TextView;  
  

/**
 * @author Ritika
 * This class is an Adaptor for showing two String in every item of the ListView.
 */
public class ResultListViewAdaptor extends BaseAdapter {
	// The name of the two columns of every item of the ListView
	private final String resultName = "resultName";
	private final String resultDistance = "resultDistance";
	
	public ArrayList<Map<String,String>> list;  
	Activity activity;  
  
	public ResultListViewAdaptor(Activity activity, ArrayList<Map<String, String>> listResults) {  
		super();  
		this.activity = activity;  
		this.list = listResults;  
	}  
  
	/**
	 * @return: the number of results.
	 */
	public int getCount() {  
		return list.size();  
	}  
   
	/**
	 * @return: the result which is at position 'position'
	 */
	public Object getItem(int position) {  
		return list.get(position);  
	}  
  
	/**
	 * @return: the result name of the result at position 'position'
	 */
	public String getItemResultName(int position) {
		return list.get(position).get(resultName);
	}
	
	public long getItemId(int position) {  
		return 0;  
	}  
  
	/**
	 * @author Ritika
	 * Holds the view of each item
	 */
	private class ViewHolder {  
		TextView txtFirst;  
		TextView txtSecond;  
	}  
	  
	/**
	 * From every result in list, grabs the two Strings to be shown in the two items, 
	 * and adds them to the View.
	 */
	public View getView(int position, View convertView, ViewGroup parent) { 
		ViewHolder holder;  
		LayoutInflater inflater = activity.getLayoutInflater();  
		  
		if (convertView == null) {
			convertView = inflater.inflate(R.layout.result_listview, null);  
			holder = new ViewHolder();  
			holder.txtFirst = (TextView) convertView.findViewById(R.id.resultString);  
			holder.txtSecond = (TextView) convertView.findViewById(R.id.distance);
			convertView.setTag(holder);  
		} else {  
			holder = (ViewHolder) convertView.getTag();  
		}  
		  
		Map<String, String> map = list.get(position);  
		holder.txtFirst.setText(map.get(resultName));  
		holder.txtSecond.setText(map.get(resultDistance)); 
		  
		return convertView;  
	}  
}  